package com.woniu.his.dms.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.woniu.his.dms.client.CaseHistoryClient;
import com.woniu.his.dms.client.param.CaseHistoryDetailResult;
import com.woniu.his.dms.client.param.DefiniteDiseParam;
import com.woniu.his.dms.client.param.DmsCaseHistoryParam;
import com.woniu.his.dms.client.result.CaseHistoryResult;
import com.woniu.his.dms.infra.model.CaseHistory;
import com.woniu.his.dms.service.CaseHistoryService;
import com.woniu.his.dms.service.dto.CaseHistoryDetailDto;
import com.woniu.his.dms.service.dto.CaseHistoryDto;
import com.woniu.his.dms.service.param.CaseHistorySvcParm;
import com.woniu.his.dms.service.param.DefiniteDiseSVParam;
import com.woniu.his.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("dms")
@Slf4j
public class CaseHistoryApiImpl implements CaseHistoryClient {


    @Resource
    private CaseHistoryService caseHistoryService;



    /**
     * @Description:  提交诊断信息（让患者进行检查或处置,生成病例id）
     * @Param:
     * @return: dx
     * @Date: 2022/11/27
     */
    @Override
    public Result<CaseHistoryResult> submitPriliminaryDise(DmsCaseHistoryParam dmsCaseHistoryParam) {

        log.info("初诊信息：{}",dmsCaseHistoryParam);

        CaseHistorySvcParm caseHistorySvcParm= BeanUtil.copyProperties(dmsCaseHistoryParam,CaseHistorySvcParm.class);
        CaseHistoryDto caseHistoryDto =caseHistoryService.submitPriliminaryDise(caseHistorySvcParm);
        CaseHistoryResult result= BeanUtil.copyProperties(caseHistoryDto,CaseHistoryResult.class);
        return Result.getInstance(CaseHistoryResult.class).setData(result).setMsg("首次诊断信息提交成功,患者进行检查处置");
    }


    /**
     * @Description: 诊毕（病人拿完药，或者处置完，或超过一定时间设置病例为诊病状态
     * @Param: dx
     * @return:
     * @Date: 2022/11/27
     */
    @Override
    public Result endDiagnosis(Long caseHistoryId) {

        log.info("病例id：{}",caseHistoryId);

        caseHistoryService.endDiagnosis(caseHistoryId);
        return  Result.getInstance().setMsg("诊毕完成");
    }

    /**
    * @Description:  提交确诊信息
    * @Param:
    * @return:
    * @Date: 2022/11/27
    */
    @Override
    public Result submitDefiniteDise(DefiniteDiseParam definiteDiseParam) {
        log.info("确诊信息参数：{}",definiteDiseParam);

        DefiniteDiseSVParam diseSVParam= BeanUtil.copyProperties(definiteDiseParam, DefiniteDiseSVParam.class);
        System.out.println(diseSVParam);
        caseHistoryService.submitDefiniteDise(diseSVParam);
        return Result.getInstance().setMsg("确诊信息提交成功,患者拿药");
    }

    /**
     * @Description:  根据病例id查询已结束就诊的历史病历（病历首页显示 诊断结果 检查项目 检查结果  拿药处方 条目 ）
     * @Param:
     * @return: dx
     * @Date: 2022/11/27
     */
    @Override
    public  Result<CaseHistoryDetailResult> selectEndCaseHistoryByReg(Long caseHistoryId) {

        log.info("病例id：{}",caseHistoryId);

        CaseHistoryDetailDto caseHistoryDetailDto=  caseHistoryService.selectEndCaseHistoryByReg(caseHistoryId);
        CaseHistoryDetailResult result= BeanUtil.copyProperties(caseHistoryDetailDto,CaseHistoryDetailResult.class);
        return Result.getInstance(CaseHistoryDetailResult.class).setData(result).setMsg("病例信息查询完毕");
    }

    @Override
    public Result<CaseHistoryResult> checkCaseHistoryExist(Long caseHistoryId) {

        log.info("传回来的病历ID:{}",caseHistoryId);
        CaseHistory one = caseHistoryService.getById(caseHistoryId);
        if (one == null) return Result.getInstance(CaseHistoryResult.class).setCode(500).setMsg("该病历不存在").setData(null);
        CaseHistoryResult result=BeanUtil.copyProperties(one,CaseHistoryResult.class);
        return Result.getInstance(CaseHistoryResult.class).setData(result);
    }



    /**
     * 根据挂号id查询病历
     * @param RegId
     * @return
     */
    @Override
    public Result<CaseHistoryResult> queryCaseHistoryByRegId(Long RegId) {
        log.info("查询病历参数挂号id：{}",RegId);
        CaseHistoryDto caseHistoryDto = caseHistoryService.queryCaseHistoryByRegId(RegId);
        return Result.getInstance(CaseHistoryResult.class).setData(BeanUtil.copyProperties(caseHistoryDto,CaseHistoryResult.class));
    }


    /**
     * @Description:  挂号时生成病历
     * @Param:
     * @return: dx
     * @Date: 2022/11/27
     */
    @Override
    public Result<CaseHistoryResult> createCaseHistoryWhenRegistration(Long patientId,Long skdId, Long registrationId) {

        log.info("挂号病人id:{}",patientId);

        CaseHistoryDto caseHistoryDto =caseHistoryService.createCaseHistoryWhenRegistration(patientId,skdId,registrationId);
        CaseHistoryResult result= BeanUtil.copyProperties(caseHistoryDto,CaseHistoryResult.class);
        return Result.getInstance(CaseHistoryResult.class).setData(result).setMsg("挂号生成病历成功");
    }

    /**
     * 通过病例id查询
     * @param id
     * @return
     */
    @Override
    public Result<CaseHistoryResult> queryById(Long id) {
        log.info("通过id查询详情:{}",id);
        CaseHistory byId = caseHistoryService.getById(id);
        if(byId==null) return null;
        return Result.getInstance(CaseHistoryResult.class).setData(BeanUtil.copyProperties(byId,CaseHistoryResult.class));
    }
}
